<template> 
	<div class="app-container">
		<el-card class="filter-container" shadow="never">
			<div>
				<i class="el-icon-search"></i>
				<span>筛选搜索</span>
				<el-button style="float:right" type="primary" @click="handleSearchList()" size="small">
					查询搜索
				</el-button>
				<el-button style="float:right;margin-right: 15px" @click="handleResetSearch()" size="small">
					重置
				</el-button>
			</div>
			<div style="margin-top: 15px">
				<el-form :inline="true" :model="listQuery" size="small" label-width="140px">
					<el-form-item label="广告名称：">
						<el-input v-model="listQuery.name" class="input-width" placeholder="广告名称"></el-input>
					</el-form-item>
					<el-form-item label="广告位置：">
						<el-select v-model="listQuery.type" placeholder="全部" clearable class="input-width">
							<el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
					<!-- <el-form-item label="到期时间：">
            <el-date-picker
              class="input-width"
              v-model="listQuery.endTime"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请选择时间">
            </el-date-picker>
          </el-form-item> -->
				</el-form>
			</div>
		</el-card>
		<el-card class="operate-container" shadow="never">
			<i class="el-icon-tickets"></i>
			<span>数据列表</span>
			<el-button size="mini" class="btn-add" @click="handleAdd()">添加</el-button>
		</el-card>
		<div class="table-container">
			<!-- @selection-change="handleSelectionChange" -->
			<el-table ref="homeAdvertiseTable" :data="list" style="width: 100%;" 
			 v-loading="listLoading" border>
				<!-- <el-table-column type="selection" width="60" align="center"></el-table-column> -->
				<el-table-column label="编号" width="120" align="center">
					<template slot-scope="scope">{{scope.row.id}}</template>
				</el-table-column>
				<el-table-column label="广告名称" align="center">
					<template slot-scope="scope">{{scope.row.name}}</template>
				</el-table-column>
				<el-table-column label="广告位置" width="120" align="center">
					<template slot-scope="scope">{{scope.row.type | formatType}}</template>
				</el-table-column>
				<el-table-column label="广告图片" width="120" align="center">
					<template slot-scope="scope"><img style="height: 80px" :src="scope.row.pic"></template>
				</el-table-column>
				<el-table-column label="长图详情" width="200" align="center">
					<template slot-scope="scope"><img style="height: 80px" :src="scope.row.url"></template>
				</el-table-column>
				<el-table-column label="分类编号" width="120" align="center">
					<template slot-scope="scope">{{scope.row.categoryId}}</template>
				</el-table-column>
				<el-table-column label="商品编号" width="120" align="center">
					<template slot-scope="scope">{{scope.row.productId}}</template>
				</el-table-column>
				<el-table-column label="文章编号" width="120" align="center">
					<template slot-scope="scope">{{scope.row.subjectId}}</template>
				</el-table-column>
				<!-- <el-table-column label="文章类型" width="120" align="center">
					<template slot-scope="scope">{{scope.row.subjectType | formatSubjectType}}</template>
				</el-table-column> -->
				<el-table-column label="排序" width="120" align="center">
					<template slot-scope="scope">{{scope.row.sort}}</template>
				</el-table-column>
				<!-- <el-table-column label="时间" width="220" align="center">
					<template slot-scope="scope">
						<p>开始时间：{{scope.row.startTime | formatTime}}</p>
						<p>到期时间：{{scope.row.endTime | formatTime}}</p>
					</template>
				</el-table-column> -->
				<el-table-column label="是否显示" width="120" align="center">
					<template slot-scope="scope">
						<el-switch @change="handleUpdateStatus(scope.$index, scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.status">
						</el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="120" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="text" @click="handleUpdate(scope.$index, scope.row)">编辑
						</el-button>
						<el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<!-- <div class="batch-operate-container">
			<el-select size="small" v-model="operateType" placeholder="批量操作">
				<el-option v-for="item in operates" :key="item.value" :label="item.label" :value="item.value">
				</el-option>
			</el-select>
			<el-button style="margin-left: 20px" class="search-button" @click="handleBatchOperate()" type="primary" size="small">
				确定
			</el-button>
		</div> -->
		<div class="pagination-container">
			<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes,prev, pager, next,jumper"
			 :page-size="listQuery.pageSize" :page-sizes="[10,15,50]" :current-page.sync="listQuery.pageNum" :total="total">
			</el-pagination>
		</div>
	</div>
</template>
<script>
	import {
		fetchList,
		updateStatus,
		deleteHomeAdvertise
	} from '@/api/homeAdvertise';
	import {
		formatDate
	} from '@/utils/date';
	// import store from '@/store';
	// const storeId = store.getters && store.getters.storeId;
	const defaultListQuery = {
		pageNum: 1,
		pageSize: 10,
		name: null,
		type: null,
		endTime: null,
	};
	const defaultTypeOptions = [{
			label: '首页-轮播图',
			value: 1
		},
		{
			label: '首页-活动弹框',
			value: 2
		},
		{
			label: '玩酷商城-轮播图',
			value: 3
		}, {
			label: '玩酷商城-积分兑换-轮播图',
			value: 4
		}, {
			label: '玩酷商城-所有分类-轮播图',
			value: 5
		}
	];
	export default {
		name: 'homeAdvertiseList',
		data() {
			return {
				listQuery: Object.assign({}, defaultListQuery),
				typeOptions: Object.assign({}, defaultTypeOptions),
				list: null,
				total: null,
				listLoading: false,
				multipleSelection: [],
				operates: [{
					label: "删除",
					value: 0
				}],
				operateType: null
			}
		},
		created() {
			this.getList();
		},
		filters: {
			formatType(type) {
				if (type === 5) {
					return '玩酷商城-所有分类-轮播图';
				} else if (type === 4) {
					return '玩酷商城-积分兑换-轮播图';
				} else if (type === 3) {
					return '玩酷商城-轮播图';
				} else if (type === 2) {
					return '首页-活动弹框';
				} else if (type === 1) {
					return '首页-轮播图';
				}
			},
			formatSubjectType(subjcetType) {
				if (subjcetType === 1) {
					return '其它';
				} else if (subjcetType === 0) {
					return '科普';
				}
			},
			formatStore(type) {
				if (type === 0) {
					return '平台轮播图';
				} else {
					return type;
				}

			},
			formatTime(time) {
				if (time == null || time === '') {
					return 'N/A';
				}
				let date = new Date(time);
				return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
			},
		},
		methods: {
			handleResetSearch() {
				this.listQuery = Object.assign({}, defaultListQuery);
			},
			handleSearchList() {
				this.listQuery.pageNum = 1;
				this.getList();
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			handleSizeChange(val) {
				this.listQuery.pageNum = 1;
				this.listQuery.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.listQuery.pageNum = val;
				this.getList();
			},
			handleUpdateStatus(index, row) {
				this.$confirm('是否要修改上线/下线状态?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					updateStatus(row.id, {
						status: row.status
					}).then(response => {
						this.getList();
						this.$message({
							type: 'success',
							message: '修改成功!'
						});
					});
				}).catch(() => {
					this.$message({
						type: 'success',
						message: '已取消操作!'
					});
					this.getList();
				});
			},
			handleDelete(index, row) {
				this.deleteHomeAdvertise(row.id);
			},
			handleBatchOperate() {
				if (this.multipleSelection < 1) {
					this.$message({
						message: '请选择一条记录',
						type: 'warning',
						duration: 1000
					});
					return;
				}
				let ids = [];
				for (let i = 0; i < this.multipleSelection.length; i++) {
					ids.push(this.multipleSelection[i].id);
				}
				if (this.operateType === 0) {
					//删除
					this.deleteHomeAdvertise(this.multipleSelection[i].id);
				} else {
					this.$message({
						message: '请选择批量操作类型',
						type: 'warning',
						duration: 1000
					});
				}
			},
			handleAdd() {
				this.$router.push({
					path: '/marking/addHomeAdvertise'
				})
			},
			handleUpdate(index, row) {
				this.$router.push({
					path: '/marking/updateHomeAdvertise',
					query: {
						id: row.id
					}
				})
			},
			getList() {
				this.listLoading = true;
				console.log(this.listQuery)
				fetchList(this.listQuery).then(response => {
					this.listLoading = false;
					this.list = response.data.records;
					this.total = response.data.total;
				})
			},
			deleteHomeAdvertise(ids) {
				this.$confirm('是否要删除该广告?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					let params = new URLSearchParams();
					params.append("ids", ids);
					deleteHomeAdvertise(ids).then(response => {
						this.getList();
						this.$message({
							type: 'success',
							message: '删除成功!'
						});
					});
				})
			}
		}
	}
</script>
<style scoped>
	.input-width {
		width: 203px;
	}
</style>
